package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.BankAdapter;
import com.xfly.app.bean.BankBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BankListActivity extends BaseMvpActivity
{

    TitleBarView title_bar;

    ApiDataRepository apiDataRepository =  ApiDataRepository.getInstance();

    int page = 1;

    PullToRefreshLayout refresh_view;
    PullableListView listview;

    BankAdapter bankAdapter;

    List<BankBean> list = new ArrayList<>();

    boolean select = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_banklist);
    }

    @Override
    public void initViews() {
        select = getIntent().getBooleanExtra("select",false);
        title_bar = findViewById(R.id.title_bar);
        refresh_view = findViewById(R.id.refresh_view);
        listview = findViewById(R.id.listview);

        title_bar.setText("我的银行卡");
        title_bar.setRightText("添加新卡");

        bankAdapter = new BankAdapter(getContext(),list);
        listview.setAdapter(bankAdapter);
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                startActivity(new Intent(getContext(),AddBankActivity.class));
            }
        });

        refresh_view.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getdata();
            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.putExtra("id",list.get(i).id);
                intent.putExtra("name",list.get(i).bank);
                intent.putExtra("logo",list.get(i).bank_logo);
                intent.putExtra("num",list.get(i).bank_card);
                setResult(2,intent);
                finish();
            }
        });

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                Map<String,String> map = new HashMap<>();
                map.put("bank_id",list.get(i).id);
                apiDataRepository.delbank(map, new ApiNetResponse<Object>() {
                    @Override
                    public void onSuccess(Object data) {
                        showMsg("删除成功");
                        page = 1;
                        getdata();
                    }
                });
                return false;
            }
        });

        getdata();
    }

    private void getdata(){
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        apiDataRepository.banklist2(map, new ApiNetResponse<List<BankBean>>() {
            @Override
            public void onSuccess(List<BankBean> data) {
                if(refresh_view!=null){
                    refresh_view.refreshFinish(PullToRefreshLayout.SUCCEED);
                    refresh_view.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if(page == 1){
                    list.clear();
                }
                list.addAll(data);
                bankAdapter.pushData(list);

            }

            @Override
            public void onFaile(String info, List<BankBean> data, Throwable throwable) {
                super.onFaile(info, data, throwable);
                if(refresh_view!=null){
                    refresh_view.refreshFinish(PullToRefreshLayout.SUCCEED);
                    refresh_view.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
            }
        });
    }
}
